package com.cn.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import com.cn.entity.DepartmentDetail;
import com.cn.service.DepartmentDetailService;

/**
 * 接受与发送部门科类详细信息
 * @author ailinfei
 *
 */
public class DepartmentDetailServlet extends HttpServlet {
	private static final String CONTENT_TYPE = "text/plain; charset=UTF-8";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse response)
			throws ServletException, IOException {
		this.entityToJson(req, response);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

	/**                                                          
	 * 输出信息
	 * @param response
	 * @param content
	 * @throws IOException                                                                                          
	 */
	private void print(HttpServletResponse response, String content)
			throws IOException {
		response.setContentType(CONTENT_TYPE);
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		PrintWriter write = response.getWriter();
		write.print(content);
		write.close();
	}

	/**                                                          
	 * 部门信息集合List转换为JSON
	 * @param req
	 * @param response
	 * @throws ServletException
	 * @throws IOException                                                                                          
	 */
	private void entityToJson(HttpServletRequest req, HttpServletResponse response)
			throws ServletException, IOException {
        JSONObject resultJSON = null;
        String did = req.getParameter("id");
        DepartmentDetailService dse = new DepartmentDetailService();
        DepartmentDetail dmDetail = dse.getDmDetail(Integer.parseInt(did));
        try{
          resultJSON=JSONObject.fromObject(dmDetail);
         //System.out.println(resultJSON.toString());
        }catch(JSONException e){
        	e.printStackTrace();
        }
        this.print(response, resultJSON.toString(1,1));
        
	}

}
